//
//  LGEditCell.m
//  celltest
//
//  Created by guomianxiang on 2017/4/7.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "LGEditCell.h"
#import "LGDCTableHead.h"
#import "UIView+LG.h"

@interface LGEditCell ()<LGInputAccessoryViewActionDelegate>

@end

@implementation LGEditCell

- (void)setContent:(LGDCCellContent *)content {
    [super setContent:content];

    self.leftAlignmentLab.hidden = content.cellTitleAlignment;
    self.rightAlignmentLab.hidden = !self.leftAlignmentLab.hidden;
    
    self.leftAlignmentLab.text = content.title;
    self.rightAlignmentLab.text = content.title;
    
//    UITextInputTraits
    self.infoTextField.placeholder = content.placehold;
    self.infoTextField.text = content.contentText;
    self.infoTextField.enabled = content.textFieldEnable;
    self.infoTextField.keyboardType = content.keyBoardType;
    if (content.textFieldEnable) {
        self.infoTextField.textColor = [UIColor whiteColor];
    }else {
        self.infoTextField.textColor = [UIColor lightGrayColor];
    }
    
    self.presentationLable.text = content.presentationText;

    self.selectBut.selected = content.select;
    if (content.cellHeight == k_Height_DefaultCell) {
        self.presentationLable.hidden = YES;
    }else{
        self.presentationLable.hidden = NO;
    }
    //updateConstraints 需要重复利用cell 的时候掉用
    [self updateConstraints];
}

- (void)selectBtnClick:(UIButton *)sender {
    self.content.select = !sender.selected;
    sender.selected = self.content.select;
}

- (void)updateConstraints {
    if (![self.content.title containsString:@"门"]) {
        self.selectBut.hidden = YES;
        self.buttonWidthConstraint.constant = 10;
    }else {
        self.selectBut.hidden = NO;
        self.buttonWidthConstraint.constant = self.frame.size.height;
    }
    [super updateConstraints];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.selectBut addTarget:self action:@selector(selectBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    self.infoTextField.editDelegate = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(cellDidBeginEditing:andIndexpath:)]) {
        [self.delegate cellDidBeginEditing:self.bottom andIndexpath:self.content.indexPath];
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    //sn码
    if ([self.content.title containsString:@"S"]){
        // 转大写
        NSString *upStr = [self.infoTextField.text uppercaseStringWithLocale:[NSLocale currentLocale]];
        self.infoTextField.text = upStr;
    }
    
    self.content.contentText = textField.text;
    
    if ([self.delegate respondsToSelector:@selector(validateWithContent:)]) {
        [self.delegate validateWithContent:self.content];
    }
}

#pragma mark - set
- (void)setDelegate:(id<LGDCDelegate>)delegate {
    super.delegate = delegate;
    self.infoTextField.inputAVActionDelegate = delegate;
}



@end
